package com.ysu.his.admin.service.impl;

import cn.hutool.json.JSONUtil;
import com.ysu.his.admin.mapper.RoleMapper;
import com.ysu.his.constant.RedisConstant;
import com.ysu.his.dto.LoginDto;
import com.ysu.his.entity.user.Role;
import com.ysu.his.entity.user.User;
import com.ysu.his.exception.HISException;
import com.ysu.his.admin.mapper.LoginMapper;
import com.ysu.his.admin.service.LoginOutService;
import com.ysu.his.vo.LoginVo;
import com.ysu.his.vo.common.ResultCodeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class LoginOutServiceImpl implements LoginOutService
{
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public LoginVo login(LoginDto loginDto)
    {
        //获取验证码
        String captcha = loginDto.getCaptcha();

        //获取验证码key
        String key = RedisConstant.USER_VALIDATACODE + loginDto.getCodeKey();

        //查询redis获取验证码
        String captchaDB = redisTemplate.opsForValue().get(key);
        if (!StringUtils.hasText(captchaDB))
        {
            throw new HISException(ResultCodeEnum.VALIDATECODE_EXPIRE);
        }

        if (!captchaDB.equals(captcha))
        {
            throw new HISException(ResultCodeEnum.VALIDATECODE_ERROR);
        }

        String workNumber = loginDto.getWorkNumber();
        //根据工号查询数据库中用户
        User user = loginMapper.selectUserByWorkNumber(workNumber);
        if (user == null)
        {
            throw new HISException(ResultCodeEnum.USER_NOT_EXIST);
        }

        //判断用户状态
        if (user.getStatus() == 0)
        {
            throw new HISException(ResultCodeEnum.USER_ISNOT_ACTIVE);
        }

        String passwordDB = user.getPassword();
        String password = DigestUtils.md5DigestAsHex(loginDto.getPassword().getBytes());

        if (!password.equals(passwordDB))
        {
            throw new HISException(ResultCodeEnum.LOGIN_ERROR);
        }


        String roleJsonStr = (String) redisTemplate.opsForHash().get(RedisConstant.ALL_ROLE, String.valueOf(user.getRoleId()));
        Role role=JSONUtil.toBean(roleJsonStr, Role.class);
        if (role == null)
        {
            role = roleMapper.selectRoleById(user.getRoleId());
        }

        //登陆成功生成token标识,放入redis中
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        redisTemplate.opsForValue().set(RedisConstant.LOGIN_USER + token, JSONUtil.toJsonStr(user)
                , RedisConstant.USER_INFO_TTL, TimeUnit.MINUTES);

        //封装LoginVo对象
        LoginVo loginVo = new LoginVo();
        BeanUtils.copyProperties(user, loginVo);
        loginVo.setToken(token);
        loginVo.setRoleName(role.getRoleName());
        return loginVo;
    }
}
